/**
 * 使用 再page中引入js
 * new AjaxRequest({
         type: "get",
         url: "https://5ae979d7531a580014142797.mockapi.io/api/v1/records",
         param: "",
         callBack:function(res){
             console.log(res);
         },
         failBack: function(){
             console.log("fdsdf");
         }
     });
 */
(function(){
    function AjaxRequest(opts){
        this.type         = opts.type || "get";
        this.url          = opts.url;
        this.param        = opts.param || {};
        this.isShowLoader = opts.isShowLoader || true;
        this.async        = opts.async || true;
        this.dataType     = opts.dataType || "json";
        this.callBack     = opts.callBack;
        this.failBack     = opts.failBack;
        this.init();
    }
    AjaxRequest.prototype = {
        //初始化
        init: function(){
            this.sendRequest();
        },
        //渲染loader
        showLoader: function(){
            if(this.isShowLoader){
                // var loader = '<div class="ajaxLoader"><div class="loader">加载中...</div></div>';
                var loader = '';
                $("body").append(loader);
            }
        },
        //隐藏loader
        hideLoader: function(){
            if(this.isShowLoader){
                $(".ajaxLoader").remove();
            }
        },
        //发送请求
        sendRequest: function(){
            var self = this;
            $.ajax({
                type: this.type,
                url: this.url,
                data: this.param,
                async:this.async,
                dataType: this.dataType,
				contentType: 'application/json',  
                beforeSend: this.showLoader(),
                success: function(res){
                    self.hideLoader();
                    if (res != null && res != "") {
                        if(self.callBack){
                            if (Object.prototype.toString.call(self.callBack) === "[object Function]") {   //Object.prototype.toString.call方法--精确判断对象的类型
                                self.callBack(res);
                            }else{
                               cc
                            }
                        }
                    }
                },
                error:function(res){
                    self.hideLoader();
                    self.failBack(res);
                }
            });
        }
    };
    window.AjaxRequest = AjaxRequest;
    function GetQueryString(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
            if(r!=null)return  unescape(r[2]); return null;
    }
    window.GetQueryString=GetQueryString;
})();